local hx__tiandao = fk.CreateSkill{
  name = "hx__tiandao",

  tags = {Skill.Compulsory},
}

hx__tiandao:addEffect(fk.TurnEnd,{
  anim_type = "negative",
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(hx__tiandao.name) and target and target == player and player:getMark("@hx__tiandao_record") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local num = player:getMark("@hx__tiandao_record")
    room:damage{
      to = player,
      damage = num,
    }
    room:setPlayerMark(player,"@hx__tiandao_record",0)
  end,
})

hx__tiandao:addEffect(fk.DamageInflicted,{
  name = "hx__tiandao",
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__tiandao.name) and target and target == player and player:usedSkillTimes(self.name,Player.HistoryGame) < 7 and player.phase == Player.NotActive
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player,{skill_name = hx__tiandao.name, prompt = "#hx__tiandao_evoke:::"..data.damage})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player,"@hx__tiandao_record",data.damage)
    data:preventDamage()
  end,
  
})

hx__tiandao:addEffect(fk.DamageInflicted,{
  name = "#hx__tiandao__turnprevent",
  priority = 2,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__tiandao.name) and target and target == player and player:usedSkillTimes(self.name) == 0 and data.damage > player.hp
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data:preventDamage()
  end,
})

hx__tiandao:addEffect(fk.AfterCardsMove,{
  name = "hx__tiandao",
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(hx__tiandao.name) or player:usedSkillTimes(self.name,Player.HistoryGame) >= 3 then return end
        for _, move in ipairs(data) do
          if move.to == player and move.toArea == Card.PlayerJudge then
            for _, info in ipairs(move.moveInfo) do
              event:setCostData(self,info.cardId)
              return true
            end
          end
        end 
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(event:getCostData(self))
    return room:askToSkillInvoke(player,{skill_name = hx__tiandao.name,prompt = "#hx__tiandao__prejudge:::"..card:toLogString()})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(event:getCostData(self))
    room:moveCardTo(card,Card.DiscardPile,nil,fk.ReasonJustMove,hx__tiandao.name,nil,true,player)
  end
})




return hx__tiandao